package com.iman.locker.view;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import com.iman.locker.R;

/**
 * 主页面
 * Created by esirong on 2015-12-04.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.lock_protected_info).setOnClickListener(this);
        findViewById(R.id.turn_on).setOnClickListener(this);
        findViewById(R.id.ll_draw).setOnClickListener(this);
        findViewById(R.id.ll_pin).setOnClickListener(this);
        findViewById(R.id.ll_face).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //检查功能是不要因权限被限制了
        if (needPermissionForBlocking(this)) {
            findViewById(R.id.app_msg_banner).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.app_msg_banner).setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 检查是不是功能运行因为未获权限而停摆
     *
     * @param context 上下文
     * @return 是，返回true
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static boolean needPermissionForBlocking(Context context) {
        try {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
                PackageManager packageManager = context.getPackageManager();
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
                AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
                int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
                return (mode != AppOpsManager.MODE_ALLOWED);
            } else {
                return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            //无法找到类名说明此版本没有可授权的操作
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.lock_protected_info:
                toAppList();
                break;
            case R.id.turn_on:
                doTurnOnScan();
                break;
            case R.id.ll_draw:
                doAuthorizeByDraw();
                break;
            case R.id.ll_pin:
                doAuthorizeByPin();
                break;
            case R.id.ll_face:
                doAuthorizeByFace();
                break;
        }
    }

    //开启权限
    private void doTurnOnScan() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            startActivity(intent);
        }
    }

    private void doAuthorizeByDraw() {
        Intent intent = new Intent(MainActivity.this, LockActivity.class);
        intent.putExtra(LockActivity.LOCK_TYPE, LockActivity.TAB_DRAW_TO_UNLOCK);
        startActivity(intent);
    }

    private void doAuthorizeByPin() {
        Intent intent = new Intent( MainActivity.this, LockActivity.class );
        intent.putExtra(LockActivity.LOCK_TYPE, LockActivity.TAB_PIN_TO_UNLOCK);
        startActivity(intent);
    }

    private void toAppList() {
        Intent intent = new Intent(this, AppsActivity.class);
        startActivity(intent);
    }

    private void doAuthorizeByFace() {
        Intent intent = new Intent(MainActivity.this, LockActivity.class);
        intent.putExtra(LockActivity.LOCK_TYPE, LockActivity.TAB_FACE_TO_UNLOCK);
        startActivity(intent);
    }
}
